﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using Oracle.DataAccess.Client;
using Sweepids.Models.Dao;

namespace Sweepids.Models.Object
{
    public class Lose
    {
        //Private members
        private bool _fill = false;
        private Item _itemObj;
        private User _userObj;
        //Columns
        public decimal ItemId { get; set; }
        public string Username { get; set; }
        //Foreign keys
        public Item ItemObj
        {
            get
            {
                if (!_fill)
                {
                    var itemDao = new ItemDao();
                    _itemObj = itemDao.Read(ItemId);
                }
                return _itemObj;
            }
            set
            {
                _itemObj = value;
                ItemId = value.Id;
            }
        }

        public User UserObj
        {
            get 
            {
                if (!_fill)
                {
                    var userDao = new UserDao();
                    _userObj = userDao.Read(Username);
                }
                return _userObj;
            }
            set
            {
                _userObj = value;
                Username = value.Username;
            }
        }
        //Oracle parameters
        public List<OracleParameter> OracleParameters
        {
            get
            {
                return new List<OracleParameter>
                    {
                        new OracleParameter(":ITEM_ID", OracleDbType.Decimal)
                            {
                                Value = ItemId
                            },
                        new OracleParameter(":USERNAME", OracleDbType.Varchar2)
                            {
                                Value = Username ?? ""
                            }
                    };
            }
        }
        //Constructors
        public Lose()
        {
            Username = "";
        }
        public Lose(IDataRecord myReader)
        {
            ItemId = myReader["ITEM_ID"] == DBNull.Value ? 0 : (decimal) myReader["ITEM_ID"];
            Username = myReader["USERNAME"] == DBNull.Value ? "" : (string) myReader["USERNAME"];
        }
        //Methods
        public void Fill()
        {
            _fill = false;
            _itemObj = ItemObj;
            _userObj = UserObj;
            _fill = true;
        }
    }
}